import ujson


def read_user(user_path):

    try:
        with open(user_path, 'r', encoding='utf-8') as f:
            content = ujson.load(f)
        return content
    except FileNotFoundError:
        print(f"错误：文件 {user_path} 不存在")
        return None
    except ujson.JSONDecodeError:
        print(f"错误：文件 {user_path} 格式不正确（非 valid JSON）")
        return None
    except Exception as e:
        print(f"读取文件 {user_path} 失败：{str(e)}")
        return None

def write_user(user_path, data):
    try:
        with open(user_path, 'w', encoding='utf-8') as f:
            ujson.dump(data, f, indent=2, ensure_ascii=False)
        return True
    except TypeError as e:
        print(f"错误：数据无法序列化（可能包含不可 JSON 序列化的类型）：{str(e)}")
        return False
    except Exception as e:
        print(f"写入文件 {user_path} 失败：{str(e)}")
        return False

